package com.sr.examination.user.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import com.sr.examination.R;
import com.sr.examination.exam.grade.ExamGradeActivity;
import com.tl.commonlibrary.event.LoginStatusEvent;
import com.sr.examination.net.Net;
import com.tl.commonlibrary.event.EventUtil;
import com.tl.commonlibrary.network.ErrorResponse;
import com.tl.commonlibrary.network.RequestListener;
import com.tl.commonlibrary.network.bean.base.BaseBean;
import com.tl.commonlibrary.tool.UIUtils;
import com.tl.commonlibrary.ui.BaseFragmentActivity;
import com.tl.commonlibrary.ui.beans.User;
import com.tl.commonlibrary.ui.user.Account;

import retrofit2.Call;

/**
 * Created by JongSung on 18/2/25.
 */

public class LoginActivity extends BaseFragmentActivity {
    public static void start(Context context) {
        Intent starter = new Intent(context, LoginActivity.class);
        context.startActivity(starter);
    }

    /**
     * 用户是上帝，如果你确定某个手机号正是用户想要登录的，为了节省输入时间，那么带过来吧，上帝会感谢你
     *
     * @param phoneNumber 上帝的手机号码
     */
    public static void start(Context context, String phoneNumber) {
        context.startActivity(new Intent(context, LoginActivity.class).putExtra("phoneNumber", phoneNumber));
    }

    private EditText phoneEText, passwordEText;
    private ImageButton clearPhoneBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_login);
        setTitle(R.string.login);
        phoneEText = findViewById(R.id.phoneEText);
        passwordEText = findViewById(R.id.passwordEText);
        findViewById(R.id.registerBtn).setOnClickListener(this);
        findViewById(R.id.forgetPasswordBtn).setOnClickListener(this);
        findViewById(R.id.loginBtn).setOnClickListener(this);
        clearPhoneBtn = findViewById(R.id.clearPhoneBtn);
        phoneEText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    clearPhoneBtn.setVisibility(View.GONE);
                } else {
                    clearPhoneBtn.setVisibility(View.VISIBLE);
                }
            }
        });
        initIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        initIntent(intent);
    }

    private void initIntent(Intent intent) {
        String phoneNumber = "";
        if (intent != null) {
            phoneNumber = intent.getStringExtra("phoneNumber");
        }
        setPhone(phoneNumber);
    }

    public void setPhone(String phoneNumber) {
        if (!TextUtils.isEmpty(phoneNumber)) {
            phoneEText.setText(phoneNumber);
            phoneEText.setSelection(phoneNumber.length());
            passwordEText.requestFocus();
        } else {
            phoneEText.setText("");
        }
    }

    private void login() {
        String mobile = phoneEText.getText().toString().trim();
        if (TextUtils.isEmpty(mobile)) {
            UIUtils.showToast(getString(R.string.hint_loin_phone_empty));
            return;
        }
        String pwd = passwordEText.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            UIUtils.showToast(getString(R.string.hint_loin_password_empty));
            return;
        }
        showProgressDialog(getString(R.string.progress_login));
        Net.login(mobile, pwd, new RequestListener<BaseBean<User>>() {
            @Override
            public void onSuccess(Call<BaseBean<User>> call, BaseBean<User> response) {
                if (response.isSuccessful()) {
                    Account.setUser(response.data);
                    UIUtils.showToast(getString(R.string.hint_login_success));
                    EventUtil.post(new LoginStatusEvent(LoginStatusEvent.LOGGIN));
                    dismissAll();
                    finish();
                } else {
                    dismissAll();
                }

            }

            @Override
            public void onFailed(Call<BaseBean<User>> call, ErrorResponse errorResponse) {
                dismissAll();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginBtn:
                login();
                break;
            case R.id.registerBtn:
                RegisterActivity.start(context);
                break;
            case R.id.forgetPasswordBtn:
                RegisterActivity.startForPassword(context);
                break;
        }
    }

    @Override
    public void onFragmentTrigger(int tag, Bundle bundle) {

    }
}
